| Conditions | 14 |
| Paths | 810 |
| Total Lines | 45 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like validate.js ➔ sourceConfiguration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | class Validate { |
||
| 10 | sourceConfiguration (input, callback) { |
||
| 11 | let error = null |
||
| 12 | |||
| 13 | if (!input.source.url) { |
||
| 14 | if (!error) { |
||
| 15 | error = new Error('URL for MQTT broker has not been set.') |
||
| 16 | } |
||
| 17 | } |
||
| 18 | |||
| 19 | if (!this.url(input.source.url)) { |
||
| 20 | if (!error) { |
||
| 21 | error = new Error('Your defined URL is invalid. It should start with mqtt://') |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | let port = Number.parseInt(input.source.port) |
||
| 26 | if (Number.isNaN(port)) { |
||
| 27 | input.source.port = 1883 |
||
| 28 | } else { |
||
| 29 | input.source.port = port |
||
| 30 | } |
||
| 31 | |||
| 32 | callback(input, error) |
||
| 33 | } |
||
| 34 | |||
| 35 | paramConfiguration (input, callback) { |
||
| 36 | let error = null |
||
| 37 | |||
| 38 | if (!input.hasOwnProperty('params')) { |
||
| 39 | if (!error) { |
||
| 40 | return error = new Error('The parameters are not been set.') |
||
| 41 | } |
||
| 42 | } else { |
||
| 43 | if (!input.params.topic) { |
||
| 44 | if (!error) { |
||
| 45 | error = new Error('The parameter topic has not been set.') |
||
| 46 | } |
||
| 47 | } |
||
| 48 | if (!input.params.payload) { |
||
| 49 | if (!error) { |
||
| 50 | error = new Error('The parameter payload has not been set.') |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | if (![0, 1, 2].includes(input.params.qos)) { |
||
| 55 | input.params.qos = 1 |
||
| 67 |